<%@page import="org.lzy.pt.bean.TaskEx"%>
<%@page import="org.jivesoftware.util.JiveGlobals"%>
<%@page import="java.util.Date"%>
<%@page import="org.lzy.pt.bean.Task"%>
<%@page import="org.lzy.pt.TaskManager"%>
<%@page import="java.util.List"%>
<%@page import="org.jivesoftware.util.LocaleUtils,
                 org.jivesoftware.util.ParamUtils"%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt"%>

<%!
    final int DEFAULT_RANGE = 1;
    final int[] RANGE_PRESETS = {25, 50, 75, 100, 500, 1000, -1};
%>

<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager" />
<% webManager.init(request, response, session, application, out ); %>
<%  // Get parameters
	String action = ParamUtils.getParameter(request,"action");
	String datetime = ParamUtils.getParameter(request,"datetime");
	String username = ParamUtils.getParameter(request,"username");
	if(action != null && action.equals("cancel")){
		TaskManager.getInstance().forceCancel(username,datetime);
	}
%>

<html>
<head>
<title><fmt:message key="item.task.summary"></fmt:message></title>
<meta name="pageID" content="task-summary" />
</head>
<body>
	<%  // Get parameters
    int start = ParamUtils.getIntParameter(request,"start",0);
    int range = ParamUtils.getIntParameter(request,"range",webManager.getRowsPerPage("task-summary", DEFAULT_RANGE));

    if (request.getParameter("range") != null) {
        webManager.setRowsPerPage("task-summary", range);
    }

    // Get the user manager
    long taskCount = TaskManager.getInstance().queryTaskNum();

    // paginator vars
    int numPages = (int)Math.ceil((double)taskCount/(double)range);
    int curPage = (start/range) + 1;
%>

	<p>
		<fmt:message key="task.summary.total_task" />
		: <b><%= LocaleUtils.getLocalizedNumber(taskCount) %></b> --

		<%  if (numPages > 1) { %>

		<fmt:message key="global.showing" />
		<%= LocaleUtils.getLocalizedNumber(start+1) %>-<%= LocaleUtils.getLocalizedNumber(start+range > taskCount ? taskCount:start+range) %>,

		<%  } %>
		--
		<fmt:message key="task.summary.tasks_per_page" />
		: <select size="1"
			onchange="location.href='task-summary.jsp?start=0&range=' + this.options[this.selectedIndex].value;">

			<% for (int aRANGE_PRESETS : RANGE_PRESETS) { %>

			<option
				value="<%  if (aRANGE_PRESETS > 0) { %><%= aRANGE_PRESETS %><%  }else{ %><%= taskCount %><%}%>"
				<%= (aRANGE_PRESETS == range ? "selected" : "") %>>
				<%  if (aRANGE_PRESETS > 0) { %><%= aRANGE_PRESETS %>
				<%  }else{ %><%= taskCount %>
				<%}%>
			</option>

			<% } %>

		</select>
	</p>

	<%  if (numPages > 1) { %>

	<p>
		<fmt:message key="global.pages" />
		: [
		<%  int num = 15 + curPage;
        int s = curPage-1;
        if (s > 5) {
            s -= 5;
        }
        if (s < 5) {
            s = 0;
        }
        if (s > 2) {
    %>
		<a href="task-summary.jsp?start=0&range=<%= range %>">1</a> ...

		<%
        }
        int i;
        for (i=s; i<numPages && i<num; i++) {
            String sep = ((i+1)<numPages) ? " " : "";
            boolean isCurrent = (i+1) == curPage;
    %>
		<a href="task-summary.jsp?start=<%= (i*range) %>&range=<%= range %>"
			class="<%= ((isCurrent) ? "jive-current" : "") %>"><%= (i+1) %></a><%= sep %>

		<%  } %>

		<%  if (i < numPages) { %>

		... <a
			href="task-summary.jsp?start=<%= ((numPages-1)*range) %>&range=<%= range %>"><%= numPages %></a>

		<%  } %>

		]

	</p>

	<%  } %>

	<div class="jive-table">
		<table cellpadding="0" cellspacing="0" border="0" width="100%">
			<thead>
				<tr>
					<th>&nbsp;</th>
					<th nowrap><fmt:message key="task.username"></fmt:message></th>
					<th nowrap><fmt:message key="task.T_DateTime"></fmt:message></th>
					<th nowrap><fmt:message key="task.T_Name"></fmt:message></th>
					<th nowrap><fmt:message key="task.T_ExecDateTime"></fmt:message></th>
<%-- 					<th nowrap><fmt:message key="task.T_Para"></fmt:message></th> --%>
					<th nowrap><fmt:message key="task.T_Cnt"></fmt:message></th>
					<th nowrap><fmt:message key="task.T_Interval"></fmt:message></th>
					<th nowrap><fmt:message key="task.T_MainVer"></fmt:message></th>
					<th nowrap><fmt:message key="task.T_FuncName"></fmt:message></th>
					<th nowrap><fmt:message key="task.T_FuncVer"></fmt:message></th>
					<th nowrap><fmt:message key="task.T_CompleteFlag"></fmt:message></th>
					<th nowrap><fmt:message key="task.action"></fmt:message></th>
				</tr>
			</thead>
			<tbody>

				<%  // Print the list of users
	List<TaskEx> taskList = TaskManager.getInstance().queryTaskList(start,range);
    if (taskList.isEmpty()) {
%>
				<tr>
					<td align="center" colspan="7"><fmt:message
							key="task.summary.not_task" /></td>
				</tr>

				<%
    }
    int i = start;
    for (TaskEx task : taskList) {
        i++;
%>
				<tr class="jive-<%= (((i%2)==0) ? "even" : "odd") %>">
					<td width="1%"><%= i %></td>
					<td><%= task.getUsername() %> &nbsp;</td>
					<td><%= task.getT_DateTime(true) %> &nbsp;</td>
					<td><%= task.getT_CnName()%> &nbsp;</td>
					<td><%= task.getT_ExecDateTime(true) %> &nbsp;</td>
<%-- 					<td><%= task.getT_Para() %> &nbsp;</td> --%>
					<td><%= task.getT_Cnt() %> &nbsp;</td>
					<td><%= task.getT_Interval() %> &nbsp;</td>
					<td><%= task.getT_MainVer() %> &nbsp;</td>
					<td><%= task.getPlugin_CnName() %> &nbsp;</td>
					<td><%= task.getT_FuncVer() %> &nbsp;</td>
					<td>
						<%=LocaleUtils.getLocalizedString("task.T_CompleteFlag."+task.getT_CompleteFlag(),"usermanage")%>
					</td>
					<%
						if(task.getT_CompleteFlag() == 1){
					%>		
							<td width="1%" align="center">
							<a href="task-summary.jsp?action=cancel&datetime=<%=task.getT_DateTime() %>&username=<%=task.getUsername() %>">
								<fmt:message key="global.cancel"></fmt:message></a>
							</td>
					<%
						}else{
					%>
							<td width="1%" align="center">-</td>
					<%
						}
					%>
				</tr>

				<%
    }
%>
			</tbody>
		</table>
	</div>

	<%  if (numPages > 1) { %>

	<p>
		<fmt:message key="global.pages" />
		: [
		<%  int num = 15 + curPage;
        int s = curPage-1;
        if (s > 5) {
            s -= 5;
        }
        if (s < 5) {
            s = 0;
        }
        if (s > 2) {
    %>
		<a href="task-manager.jsp?start=0&range=<%= range %>">1</a> ...

		<%
        }
        for (i=s; i<numPages && i<num; i++) {
            String sep = ((i+1)<numPages) ? " " : "";
            boolean isCurrent = (i+1) == curPage;
    %>
		<a href="task-manager.jsp?start=<%= (i*range) %>&range=<%= range %>"
			class="<%= ((isCurrent) ? "jive-current" : "") %>"><%= (i+1) %></a><%= sep %>

		<%  } %>

		<%  if (i < numPages) { %>

		... <a
			href="task-manager.jsp?start=<%= ((numPages-1)*range) %>&range=<%= range %>"><%= numPages %></a>

		<%  } %>

		]

	</p>

	<%  } %>

</body>
</html>
